<?php

/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Base.php');

class BH_RSS_Widget extends BH_Controllable_Widget_Base {

	function widget($args) {
		require_once(ABSPATH . WPINC . '/rss-functions.php');
		require_once(ABSPATH . WPINC . '/cache.php');
		extract($args);
	
		$title = $this->get_attribute('title');
		$num_items = (int) $this->get_attribute('items');
		$url = $this->get_attribute('url');
		$expire  = $this->get_attribute('url');
	
		if ( empty($num_items) || $num_items < 1 )
			$num_items = 10;
	
		while ( strstr($url, 'http') != $url )
			$url = substr($url, 1);

		if ( empty($url) ) {
			echo "<!-- ". $this->get_name(). " URL is empty -->\n";
			return;
		}
			
		
		if (!defined('ENABLE_CACHE') || !ENABLE_CACHE)
			wp_cache_init();
			
		$key = 'bh-widget:'.get_class($this).':'. $this->get_name();
			
		$rss = wp_cache_get($key);
		
		if($rss === false) {
			$rss = fetch_rss($url);
			wp_cache_set($key, $rss, '', $expire);
		}
		
		if (empty($rss) ) {
			echo "<!-- ". $this->get_name(). " ($url) RSS is empty -->\n";
			return;
		}
	
		$link = wp_specialchars(strip_tags($rss->channel['link']), 1);
	
		while ( strstr($link, 'http') != $link )
			$link = substr($link, 1);
		
		$desc = wp_specialchars(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)), 1);


		if ( empty($title) )
			$title = htmlentities(strip_tags($rss->channel['title']));

		if ( empty($title) )
			$title = __('Untitled');

		$url = wp_specialchars(strip_tags($url), 1);

		if ( file_exists(dirname(__FILE__) . '/rss.png') )
			$icon = str_replace(ABSPATH, get_settings('siteurl').'/', dirname(__FILE__)) . '/rss.png';
		else
			$icon = get_settings('siteurl').'/wp-includes/images/rss.png';

		$title = "<a class='rsswidget' href='$url' title='Syndicate this content'><img width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";

		echo "$before_widget\n";
		echo $this->get_widget_start_tag($args);
	
		if ($title)  print $before_title . $title . $after_title ."\n";
		
		if ( $rss ) {
		    echo "$before_list\n";
			$rss->items = array_slice($rss->items, 0, $num_items);

			foreach ($rss->items as $item ) {
				while ( strstr($item['link'], 'http') != $item['link'] )
					$item['link'] = substr($item['link'], 1);

				$link = wp_specialchars(strip_tags($item['link']), 1);
				$title = wp_specialchars(strip_tags($item['title']), 1);
					

				echo $before_item ."<a href=\"$link\" title='$desc'>$title</a>" . $after_item . "\n";			
			}
			
			echo "$after_list\n";
		} else {
			echo $before_item. "An error has occured; the feed is probably down. Try again later" . $after_item . "\n";
		}
		
		echo $this->get_widget_end_tag($args);

		echo "$after_widget\n";
	}

	function control() {
	
		if ( $this->get_request_param_value('submit') ) {
			$this->update_attribute_from_param('title');
			$this->update_attribute_from_param('items');
			$this->update_attribute_from_param('url');
		}
		
		$title = $this->get_attribute('title');
		$items = (int) $this->get_attribute('items');
		$url = $this->get_attribute('url');
		$show_summary = $this->get_attribute('show-summary');

		if ( empty($items) || $items < 1 )
			$items = 10;
			
			echo "<label>Title(optional):</label><input name=\"". $this->get_request_param_name('title') ."\" type=\"text\" value=\"$title\" /><br/>\n";
			echo "<label><?php _e('Number of items to display:'); ?></label>\n";
			echo "<label>URL:</label><input name=\"". $this->get_request_param_name('url') ."\" type=\"text\" value=\"$url\" /><br/>\n";
			echo "<label>Items:</label><input name=\"". $this->get_request_param_name('items') ."\" type=\"text\" value=\"$items\" /><br/>\n";
			echo "<input type=\"hidden\" name=\"". $this->get_request_param_name('submit') ."\" value=\"1\" />";

	}
}

bh_widgets_register_class('BH_RSS_Widget', 320, 200);
?>
